package com.zl.smartmall.library.os;

import java.io.File;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.zl.smartmall.library.concurrent.ThreadExecutor;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;
import com.zl.smartmall.library.util.Tools;

/**
 * 打扫服务：此类的功能为清除app里多余的垃圾文件
 * @author 陈希然　
 */
public class CleanService extends Service {

	private static String TAG = CleanService.class.getSimpleName();

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		clean();
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		CrashHandler.d(TAG, "打扫服务销毁");
	}
	
	private void clean() {
		ThreadExecutor.getInstance().execute(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				String dirPicturePath = Tools.obtainDirPath(getApplicationContext(), Constants.FOLDER_IMG_TEMP_CACHE);
				File dirPicture = new File(dirPicturePath);
				if(dirPicture.isDirectory()) {
					for(File file : dirPicture.listFiles()) {
						CrashHandler.d(TAG, "删除的文件 = " + file.getAbsolutePath());
						file.delete();
					}
				}
				
				stopSelf();
			}
		});
		
	}
	
}
